<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/11
 * Time: 10:23
 */

namespace app\applet\controller;


use app\share\model\Token;
use think\Validate;

class StoreOrder
{

    public function __construct(){
        $this->store=new \app\applet\model\StoreOrder();
        $this->token=new Token();
    }

    /**
     * 商城订单页面
     */
    public function shoppingMall(){
        $member_id=get_header_id('token');

        $store_id=$this->store->selectStore($member_id);

        $returnData=$this->store->getCount($store_id);

        returnJson(200,'success',$returnData);

    }

    /**
     * 订单列表   待发货   待收货
     */
    public function orderList(){
        $param=input();
        $member_id = get_header_id("token");
        //$member_id=70;
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $type=empty($param['type']) ? 1: $param['type'];
        $status=empty($param['status']) ? 1: $param['status'];
        $reply_type=empty($param['reply_type']) ? 1: $param['reply_type'];

        $store_id=$this->store->selectStore($member_id);

        $returnData=$this->store->orderList($store_id,$type,$page,$page_size,$status,$reply_type);
        //$returnData=$this->store->orderList($member_id,$type,$page,$page_size);
        returnJson(200,'success',$returnData);
    }
    /**
     * 订单列表   售后管理    status 售后类型  1=全部  2=退款 3=退货退款
     */
    public function afterList(){
        $param=input();
        $member_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $status=empty($param['status']) ? 1: $param['status'];
        $type=3;

        $store_id=$this->store->selectStore($member_id);
        //$store_id=69;

        $returnData=$this->store->orderList($store_id,$type,$page,$page_size,$status);
        returnJson(200,'success',$returnData);
    }
    /**
     * 订单列表   评价管理   reply 评价类型  2=未回复  1=已回复
     */
    public function commentList(){
        $param=input();
        $member_id = get_header_id("token");
        //$member_id=70;
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $reply_type=empty($param['reply_type']) ? 1: $param['reply_type'];
        $type=4;

        $store_id=$this->store->selectStore($member_id);

        $returnData=$this->store->orderList($store_id,$type,$page,$page_size,1,$reply_type);
        returnJson(200,'success',$returnData);
    }

    /**
     * 商家回复评价
     */
    public function replyComment(){
        $param=input();

        $validate = new Validate([
            ['c_id', 'require', '评价id不能为空'],
            ['content', 'require', '回复内容不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");
        //$member_id=70;
        $store_id=$this->store->selectStore($member_id);

        $this->store->reply($store_id,$param['c_id'],$param['content']);

        returnJson(200,'提交成功');

    }

    /**
     * 商家发货页面
     */
    public function delivery(){
        $order_id=input('order_id');
        $member_id = get_header_id("token");
        //$member_id=70;
        $store_id=$this->store->selectStore($member_id);


        $data=$this->store->deliveryPage($order_id,$store_id);

        returnJson(200,'success',$data);

    }

    /**
     * 商家发货提交
     */
    public function deliverySubmit(){
        $param=input();

        $validate = new Validate([
            ['order_id', 'require', '订单id不能为空'],
            ['log_company', 'require', '物流公司不能为空'],
            ['log_sn', 'require', '物流单号不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        $member_id = get_header_id("token");
        //$member_id=70;
        $store_id=$this->store->selectStore($member_id);


        $data['log_id']=$param['log_id'];
        $data['log_sn']=$param['log_sn'];
        $data['log_company']=$param['log_company'];
        $data['status']=3;
        $data['update_time']=time();

        $this->store->deliveryPost($param['order_id'],$store_id,$data);

        returnJson(200,'提交成功');
    }

    /**
     * 售后订单详情
     */
    public function afterDetail(){
        $after_id=input('oas_id');
        $member_id = get_header_id("token");
        $store_id=$this->store->selectStore($member_id);
        //$store_id=69;


        $data=$this->store->orderDetail($after_id,$store_id);

        returnJson(200,'success',$data);
    }

    /**
     * 售后-----退款  退货 操作
     *      status  1=同意  2=拒绝
     */
    public function after(){
        $param=input();
        $validate = new Validate([
            ['oas_id', 'require', '售后订单id不能为空'],
            ['status', 'require', '操作状态不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }

        if($param['status']==2){

            if(!isset($param['refuse_reason']) || empty($param['refuse_reason'])){
                returnJson(-2003,'拒绝原因不能为空');
            }
        }


        $member_id=get_header_id('token');
        //$member_id=69;
        $store_id=$this->store->selectStore($member_id);


        $this->store->afterRefund($store_id,$param);

        returnJson(200,'操作成功');

    }


}